package com.htjx.mall.commons.models;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import com.htjx.mall.commons.models.enums.OrderStatus;
import com.htjx.mall.commons.models.enums.TradeStatus;
import com.htjx.mall.commons.models.user.Account;


/**
 * Order 用来记录单个商品
 * OrderGroup 用来记录多个 Order 
 * @author Administrator
 *
 */
public class OrderGroup {
	/** id **/
	private long id;
	/** 用户id **/
	private long user_id;
	/** 交易状态 **/
	private TradeStatus trade_status;
	/** 商品价格总和 **/
	private BigDecimal total_price = BigDecimal.ZERO;
	/** 账户中现金使用额度 **/
	private boolean use_cash;
	private BigDecimal cash_used = BigDecimal.ZERO;
	/** 账户中现金使用额度 **/
	private boolean use_gold_coin;
	private int gold_coin_used = 0;
	/** 账户中抵用券使用额度 **/
	private boolean use_ticket;
	private BigDecimal ticket_used = BigDecimal.ZERO;
	/** 运费 **/
	private boolean free_express_cost;
	private BigDecimal express_cost = BigDecimal.ZERO;
	/** 支付额度 **/
	private BigDecimal pay_amount = BigDecimal.ZERO;
	/** 返利额度 **/
	private BigDecimal rebate = BigDecimal.ZERO;
	
	private Date create_time;
	private Date update_time;
	
	private long alipay_trade_id;   //
	
	/**版本字段**/
	private int version;
	
	private List<Order> orderList = null;
	private Account account = null;
	
	
	
	public long getId() {
		return id;
	}
	public void setId(long id) {
		this.id = id;
	}
	public TradeStatus getTrade_status() {
		return trade_status;
	}
	public void setTrade_status(TradeStatus trade_status) {
		this.trade_status = trade_status;
	}
	public BigDecimal getTotal_price() {
		return total_price;
	}
	public void setTotal_price(BigDecimal total_price) {
		this.total_price = total_price;
	}
	public boolean isUse_cash() {
		return use_cash;
	}
	public void setUse_cash(boolean use_cash) {
		this.use_cash = use_cash;
	}
	public BigDecimal getCash_used() {
		return cash_used;
	}
	public void setCash_used(BigDecimal cash_used) {
		this.cash_used = cash_used;
	}
	public boolean isUse_gold_coin() {
		return use_gold_coin;
	}
	public void setUse_gold_coin(boolean use_gold_coin) {
		this.use_gold_coin = use_gold_coin;
	}
	public int getGold_coin_used() {
		return gold_coin_used;
	}
	public void setGold_coin_used(int gold_coin_used) {
		this.gold_coin_used = gold_coin_used;
	}
	public boolean isUse_ticket() {
		return use_ticket;
	}
	public void setUse_ticket(boolean use_ticket) {
		this.use_ticket = use_ticket;
	}
	public BigDecimal getTicket_used() {
		return ticket_used;
	}
	public void setTicket_used(BigDecimal ticket_used) {
		this.ticket_used = ticket_used;
	}
	public boolean isFree_express_cost() {
		return free_express_cost;
	}
	public void setFree_express_cost(boolean free_express_cost) {
		this.free_express_cost = free_express_cost;
	}
	public BigDecimal getExpress_cost() {
		return express_cost;
	}
	public void setExpress_cost(BigDecimal express_cost) {
		this.express_cost = express_cost;
	}
	public BigDecimal getPay_amount() {
		return pay_amount;
	}
	public BigDecimal getRebate() {
		return rebate;
	}
	public void setRebate(BigDecimal rebate) {
		this.rebate = rebate;
	}
	public void setPay_amount(BigDecimal pay_amount) {
		this.pay_amount = pay_amount;
	}
	public Date getCreate_time() {
		return create_time;
	}
	public void setCreate_time(Date create_time) {
		this.create_time = create_time;
	}
	public Date getUpdate_time() {
		return update_time;
	}
	public void setUpdate_time(Date update_time) {
		this.update_time = update_time;
	}
	public List<Order> getOrderList() {
		return orderList;
	}
	public long getAlipay_trade_id() {
		return alipay_trade_id;
	}
	public void setAlipay_trade_id(long alipay_trade_id) {
		this.alipay_trade_id = alipay_trade_id;
	}
	public int getVersion() {
		return version;
	}
	public void setVersion(int version) {
		this.version = version;
	}
	public void setOrderList(List<Order> orderList) {
		this.orderList = orderList;
	}
	public Account getAccount() {
		return account;
	}
	public void setAccount(Account account) {
		this.account = account;
	}
	
	public String getSubject() {
		String subject = "";
		if(this.orderList == null) {
			return subject;
		}
		// 计算商品总价和应付总额
		for(Order order : this.orderList) {
			subject += order.getProduct_name() + " "+order.getCatalog_name() + " "+order.getSpecification_name() + "\n";
		}
		if(subject.length() > 256) {
			subject = subject.substring(0, 256);
		}
		return subject;
	}
	
	public long getUser_id() {
		return user_id;
	}
	public void setUser_id(long user_id) {
		this.user_id = user_id;
	}
	public void setAccountFrozen() {
		if(this.account == null) {
			return;
		}
		
		if(this.trade_status == TradeStatus.wait_receiving) {
			if(this.use_cash) {
				this.account.setCash(this.account.getCash().add(this.cash_used.negate()));
				this.account.setFrozen_cash(this.account.getFrozen_cash().add(this.cash_used));
			} 
			
			if(this.use_gold_coin) {
				this.account.setGold_coin(this.account.getGold_coin() - this.gold_coin_used);
				this.account.setFrozen_gold_coin(this.account.getFrozen_gold_coin() + this.gold_coin_used);
			}
		} else if(this.trade_status == TradeStatus.finish) {
			if(this.use_cash) {
				this.account.setFrozen_cash(this.account.getFrozen_cash().add(this.cash_used.negate()));
			} 
			
			if(this.use_gold_coin) {
				this.account.setFrozen_gold_coin(this.account.getFrozen_gold_coin() - this.gold_coin_used);
			}
			
			this.account.setCash(this.account.getCash().add(this.rebate));
			
		} else if(this.trade_status == TradeStatus.break_off) {
			if(this.use_cash) {
				this.account.setFrozen_cash(this.account.getFrozen_cash().add(this.cash_used.negate()));
				this.account.setCash(this.account.getCash().add(this.cash_used));
			} 
			
			if(this.use_gold_coin) {
				this.account.setFrozen_gold_coin(this.account.getFrozen_gold_coin() - this.gold_coin_used);
				this.account.setGold_coin(this.account.getGold_coin() + this.gold_coin_used);
			}
		}
	}
	
	public void setOrderListStatus() {
		if(this.orderList == null) {
			return;
		}
		if(this.trade_status == TradeStatus.wait_paying) {
			for(Order order : this.orderList) {
				order.setUpdate_time(new Date());
				order.setOrder_status(OrderStatus.paying);
			}
		} else if(this.trade_status == TradeStatus.wait_receiving) {
			for(Order order : this.orderList) {
				order.setUpdate_time(new Date());
				order.setOrder_status(OrderStatus.paid);
			}
		} else if(this.trade_status == TradeStatus.finish) {
			for(Order order : this.orderList) {
				order.setUpdate_time(new Date());
				order.setOrder_status(OrderStatus.delivering);
			}
		}
	}
	
	public void calc() {
		this.total_price = BigDecimal.ZERO;
		this.cash_used = BigDecimal.ZERO;
		this.ticket_used = BigDecimal.ZERO;
		this.gold_coin_used = 0;
		// 运费
		this.express_cost = BigDecimal.ZERO;
		this.pay_amount = BigDecimal.ZERO;
		this.rebate = BigDecimal.ZERO;
		
		if(this.orderList == null) {
			return;
		}
		// 计算商品总价和应付总额
		for(Order order : this.orderList) {
			this.total_price = this.total_price.add(order.getPrice().multiply(new BigDecimal(order.getQuantity())));
			this.pay_amount = this.total_price;
		}
		// 计算运费
		if(! this.free_express_cost) {
			this.pay_amount = this.pay_amount.add(this.express_cost);
		}
		
		if(this.account == null) {
			return;
		}
		
		// 计算金币并抵消
		if(this.use_gold_coin) {
			int gold_coin_allowed = this.pay_amount.divide(new BigDecimal(100)).intValue()*5;
			if(this.account.getGold_coin() < gold_coin_allowed) {
				this.gold_coin_used = account.getGold_coin();
			} else {
				this.gold_coin_used = gold_coin_allowed;
			}
			this.pay_amount = this.pay_amount.add(new BigDecimal(this.gold_coin_used).negate());
		}
		
		// 计算余额并抵消
		if(this.use_cash) {
			if(this.account.getCash().compareTo(this.pay_amount) > 0) {
				this.cash_used = this.pay_amount;
			} else {
				this.cash_used = this.account.getCash();
			}
			this.pay_amount = this.pay_amount.add(this.cash_used.negate());
		}
		
		// 计算抵用券并抵消
		if(this.use_ticket) {
			this.pay_amount = this.pay_amount.add(this.ticket_used.negate());
		}
		
		// 计算返利，每100元返利3元
		// 20141002 取消返利
		//this.rebate = this.pay_amount.divide(new BigDecimal(100), 0, BigDecimal.ROUND_FLOOR).multiply(new BigDecimal(3));
	}
}
